package com.supermap.panes;

import com.supermap.Helper;
import com.supermap.consts.AttrConsts;
import com.supermap.data.*;
import com.supermap.dialogs.JoinTableSettingDialog;
import com.supermap.mapping.Layer;
import com.supermap.panes.advance.SettingPane;

import javax.swing.*;
import java.awt.*;

public class LayerPropertiesSettingPane extends SettingPane {
    private Layer layer;

    public LayerPropertiesSettingPane(String title, Layer layer) {
        super(title, new Object[]{layer});
    }

    @Override
    protected void initializeArgs(Object[] args) {
        this.layer = (Layer) args[0];
    }

    @Override
    protected void initializeCore() {
        super.confirmBtn.setVisible(false);

        // 图层矢量参数相关设置
        Dataset dataset = layer.getDataset();
        if (dataset instanceof DatasetVector) {
            JPanel vectorParametersPanel = new JPanel();
            vectorParametersPanel.setBorder(BorderFactory.createTitledBorder("矢量参数"));
            vectorParametersPanel.setLayout(new GridBagLayout());
            super.mainPanel.add(vectorParametersPanel);

            JButton setJoinDataForLayerBtn = new JButton("设置图层关联属性表");
            vectorParametersPanel.add(setJoinDataForLayerBtn, new GridBagConstraints(0, 0, 1, 1, 1, 1,
                    GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));
            setJoinDataForLayerBtn.addActionListener(e -> {
                JoinTableSettingDialog joinTableSettingDialog = new JoinTableSettingDialog((Frame) getRootPane().getParent(),
                        "连接表设置", true, dataset.getDatasource(), (DatasetVector) dataset);
                joinTableSettingDialog.setVisible(true);
                JoinItem joinItem = joinTableSettingDialog.getJoinItem();
                if (joinItem != null) {
                    QueryParameter queryParameter = new QueryParameter();
                    queryParameter.setCursorType(CursorType.STATIC);
                    queryParameter.setHasGeometry(true);
                    queryParameter.setResultFields(new String[]{AttrConsts.WILDCARD});
                    queryParameter.setAttributeFilter(AttrConsts.EMPTY);
                    queryParameter.setOrderBy(new String[0]);
                    queryParameter.setGroupBy(new String[0]);
                    JoinItems joinItems = new JoinItems();
                    joinItems.add(joinItem);
                    queryParameter.setJoinItems(joinItems);
                    layer.setDisplayFilter(queryParameter);
                }
            });
        }
    }

    @Override
    protected void doConfirm() {

    }

    @Override
    protected void doClose() {

    }
}
